package netty.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;


/**
 * @author 帅小伙
 * @date 2022/1/18
 * @description
 */
public class MyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if(evt instanceof IdleStateEvent){
            // 转型
            IdleStateEvent event = (IdleStateEvent) evt;
            String evertType = null;
            switch(event.state()){
                case  READER_IDLE:
                    evertType = "读空闲";
                    break;
                case  WRITER_IDLE:
                    evertType = "写空闲";
                    break;
                case ALL_IDLE:
                    evertType = "读写空闲";
                    break;
            }

            System.out.println(ctx.channel().remoteAddress()+"发生了超时事件---:"+evertType);
            System.out.println("服务器做相应处理");
            ctx.channel().close();
        }
    }
}
